package com.hst.capacity.infra.persistence.jpa.driver;

import com.hst.capacity.domain.model.entity.driver.DriverProfileEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;


public interface DriverProfileRepository extends JpaRepository<DriverProfileEntity, String>, JpaSpecificationExecutor<DriverProfileEntity> {


    DriverProfileEntity findByIdDriverProfile(String idDriverProfile);

    /**
     * 查询司机详细信息
     * @return
     */
    @Query("select dp " +
            "from DriverEntity d " +
            "inner join DriverProfileEntity dp on d.idDriverProfile = dp.idDriverProfile " +
            "where d.idDriver=?1")
    DriverProfileEntity findByIdDriver(String idDriver);

    /**
     * 查询司机详细信息
     * @return
     */
    @Query("select dp " +
            "from DriverEntity d " +
            "inner join DriverProfileEntity dp on d.idDriverProfile = dp.idDriverProfile " +
            "where dp.idCardNo = ?1")
    DriverProfileEntity findByIdCardNo(String idCardNo);

    /**
     * 更新司机二维码
     * @param qrcode
     * @param idDriverProfile
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Modifying
    @Query("UPDATE DriverProfileEntity dp SET dp.qrcode = :qrcode " +
            " where dp.idDriverProfile=:idDriverProfile")
    int updateQrcode(String qrcode, String idDriverProfile);
}
